package com.example.file_explorer.controller;

import com.example.file_explorer.dto.CopyFileDto;
import com.example.file_explorer.dto.FileDto;
import com.example.file_explorer.entity.FileNode;
import com.example.file_explorer.result.Code;
import com.example.file_explorer.result.Result;
import com.example.file_explorer.utils.IOUtils;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Set;

@RestController
public class ExplorerController {

    @GetMapping("/fileList")
    public Set<FileNode> fileList(@RequestParam String path) throws IOException {
        return IOUtils.getFileList("../root" + path);
    }

    @GetMapping("/initNode")
    public Result<FileNode> initNode() throws IOException {
        return Result.success(IOUtils.initFileTree());
    }

    @GetMapping("/children")
    public Result<Set<FileNode>> children(@RequestParam String path) {
        Set<FileNode> directory = IOUtils.directoryNoChildren(path);
        if (directory == null) {
            return Result.error(Code.NOT_FOUND_FILE.getCode(),"未找到该文件夹");
        }
        return Result.success(directory);
    }

    @PostMapping("/addDirectory")
    public Result<Object> addDirectory(@RequestBody FileDto fileDto) {
        boolean flag = IOUtils.addDirectory(fileDto.getPath(), fileDto.getFileName());
        if (flag) {
            return Result.success();
        }else {
            return Result.error(Code.FAIL_ADD_FILE.getCode(), "文件已存在，添加失败");
        }
    }

    @GetMapping("/downloadFile")
    public Result<Object> download(@RequestParam String path, String fileName, HttpServletResponse resp) throws IOException{
        Code code = IOUtils.download(path, fileName, resp);
        if (code == Code.NOT_FOUND_FILE) {
            return Result.error(Code.NOT_FOUND_FILE.getCode(), "文件未找到");
        }
        if (code == Code.FAIL_DOWNLOAD_FILE) {
            return Result.error(Code.NOT_FOUND_FILE.getCode(), "文件下载失败");
        }
        return Result.success();
    }

    @DeleteMapping("/deleteFile")
    public Result<Object> delete(@RequestParam String path, String fileName) {
        Code code = IOUtils.deleteFile(path, fileName);
        if (code == Code.NOT_FOUND_FILE) {
            return Result.error(Code.NOT_FOUND_FILE.getCode(), "文件未找到");
        }
        if (code == Code.FAIL_DELETE_FILE) {
            return Result.error(Code.NOT_FOUND_FILE.getCode(), "文件删除失败");
        }
        return Result.success();
    }

    @PutMapping("/renameFile")
    public Result<Object> renameFile(@RequestBody FileDto fileDto) {
        Code code = IOUtils.fileRename(fileDto.getPath(), fileDto.getFileName(), fileDto.getNewName());
        if (code == Code.SUCCESS) {
            return Result.success();
        }else if (code == Code.FILE_EXIST) {
            return Result.error(Code.FILE_EXIST.getCode(), "已存在同名文件");
        }else if (code == Code.NOT_FOUND_FILE) {
            return Result.error(Code.NOT_FOUND_FILE.getCode(), "文件未找到");
        }else if (code == Code.FAIL_RENAME_FILE) {
            return Result.error(Code.FAIL_RENAME_FILE.getCode(), "文件重命名失败");
        }
        return Result.error(Code.UNKNOWN_ERROR.getCode(), "未知错误");
    }

    @PostMapping("/upload")
    public Result<Object> upload(@RequestParam String path, MultipartFile file) throws IOException{
        Code code = IOUtils.fileUpload(path, file);
        if (code == Code.NOT_FOUND_DIRECTORY) {
            return Result.error(Code.NOT_FOUND_DIRECTORY.getCode(), "未找到文件夹");
        } else if (code == Code.FILE_EXIST) {
            return Result.error(Code.FILE_EXIST.getCode(), "已存在同名文件");
        }else {
            return Result.success();
        }
    }

    @PutMapping("/copy")
    public Result<Object> copy(@RequestBody CopyFileDto copyFileDto) throws IOException{
        Code code = IOUtils.copy(copyFileDto.getSrcPath(), copyFileDto.getDescPath(), copyFileDto.getFilename());
        if (code == Code.NOT_FOUND_FILE) {
            return Result.error(Code.NOT_FOUND_FILE.getCode(), "未找到文件");
        }else if (code == Code.NOT_FOUND_DIRECTORY) {
            return Result.error(Code.NOT_FOUND_DIRECTORY.getCode(), "未找到目录");
        }else if (code == Code.FILE_EXIST) {
            return Result.error(Code.FILE_EXIST.getCode(), "目标文件夹已存在同名文件");
        }
        return Result.success();
    }
}